<?php

namespace app\admin\controller\visit;

use app\admin\model\Users;
use app\admin\model\visit\Visiting;
use app\common\controller\Backend;
use think\Db;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Data extends Backend
{

    /**
     * Visiting模型对象
     * @var \app\admin\model\yuyurVisiting
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->visitime = config("visit_time");
        $this->name =Users::field("name,id")->select();
        $this->model = new \app\admin\model\visit\Visiting;
        $this->assign("name",$this->name );
        $this->assign("vistime",$this->visitime);

    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */



    /**
     * 查看
     */
    public function index()
    {
        if($this->request->isPost()){
            $start = $this->request->param('s'); //开始时间
            $end = $this->request->param('e');  //结束时间
            if(strtotime("$start")>strtotime("$end")){
               echo "<script> alert('结束时间大于开始时间');location.href = history.go(-1);;</script>";
            }
            $result = $this->sjtj($start,$end);
            $this->assign("ydata",$result['ydata']);
            $this->assign("ddata",$result['ddata']);
            $this->assign("visit",$result['visit']);
            return $this->view->fetch();
        }
       $result =  $this->sjtj("","");
        $this->assign("ydata",$result['ydata']);
        $this->assign("ddata",$result['ddata']);
        $this->assign("visit",$result['visit']);
       return $this->view->fetch();
    }
    /**
     * 统计查询
     */
    public function sjtj($start,$end){
        $year =  date('Y');
        $xyear =  $year+1;
        $xyear = "$xyear";
        $ydata ="";
        $ddata ="";
        $ydd=[];
        $ytime = [
            "$year-01" =>[] , "$year-02" =>[] , "$year-03" =>[] , "$year-04" =>[] , "$year-05" =>[] , "$year-06" =>[] , "$year-07" =>[] , "$year-08" =>[] , "$year-09" =>[] , "$year-10" =>[], "$year-11" =>[], "$year-12" =>[]];
        if($start && $end){
            $visit_list = objToArray(Visiting::whereTime('day', 'between', [$start, $end])->field('sum(views) views ,sum(actual_views) actual_views ')->find());
            $yd = Db::query("SELECT DATE_FORMAT(day,'%Y-%m') as time,sum(views)  views ,sum(actual_views) actual_views FROM fa_visiting where day between '$start 00:00:00' and '$end 23:59:59' GROUP BY day ");
//            dump($yd);
        }else{
            $visit_list = objToArray(Visiting::whereTime('day', 'between', [$year."-1-1", $xyear."-1-1"])->field('sum(views) views ,sum(actual_views) actual_views ')->find());
            $yd = Db::query("SELECT DATE_FORMAT(day,'%Y-%m') as time,sum(views)  views ,sum(actual_views) actual_views FROM fa_visiting where day between '$year-01-01 00:00:00' and '$xyear-01-01 23:59:59' GROUP BY day ");

        }

       foreach($yd as $k=>$value){
           $ydd[$value['time']]['views'] = $ydd[$value['time']]['views']??0;
           $ydd[$value['time']]['actual_views'] = $ydd[$value['time']]['actual_views']??0;

           $ydd[$value['time']]['views']  +=  ($value['views']??0);
           $ydd[$value['time']]['actual_views']  +=  ($value['actual_views']??0);
        }

        $ydd = array_merge($ytime,$ydd);
       foreach($ydd as $k=>$value){
            $ydata .= isset($value['views']) ? $value['views']."," : "0,";
            $ddata .=isset($value['actual_views']) ? $value['actual_views']."," : "0,";
        }
//        dd($ydd);
        $ydata = rtrim($ydata,",");
        $ddata = rtrim($ddata,",");
        if(!isset($visit_list)){
            return true;
        }else{
            $visit_lv = round($visit_list['actual_views']/$visit_list['views'],2)*100;
            $visit_list['visit_lv'] =  $visit_lv;
            $result = [
                'ydata' =>$ydata,
                'ddata' => $ddata,
                'visit' =>$visit_list
            ];
        }


        return $result;
    }

}
